﻿package com.jxl.dnd.combat
{
	import flash.events.EventDispatcher;
	
	
	public class HitPoints extends EventDispatcher
	{
		
		[Bindable]
		public var hps:Number;
		public var realDamage:Number;
		public var temporaryDamage:Number;
		public var maxHitPoints:Number;
		
		
		public function get status():String
		{
			var theStatus:String = "unknown";
			if(hps > 0)
			{
				theStatus = "normal";
			}
			else if(hps == 0)
			{
				theStatus = "unconcious";
			}
			else if(hps < 0 && hps > -10)
			{
				theStatus = "dying";
			}
			else if(hps < -9)
			{
				theStatus = "dead";
			}
			return theStatus;
		}
		
		public function HitPoints(current:Number=0, max:Number=0, real:Number=0, temp:Number=0):void
		{
			hps 					= current;
			maxHitPoints 				= max;
			realDamage 				= real;
			temporaryDamage 			= temp;
		}
		
		public function doRealDamage(val:Number):void
		{
			realDamage += val;
			var result:Number = maxHitPoints - realDamage - temporaryDamage;
			hps = Math.max(result, -10);
		}
		
		public function doTemporaryDamage(val:Number):void
		{
			temporaryDamage += val;
			var result:Number = maxHitPoints - realDamage - temporaryDamage;
			hps = Math.max(result, -10);
		}
		
		public function toXML():XML
		{
			var hpXML:XML = 
				<hitpoints>
					<hps>{hps}</hps>
					<realdamage>{realDamage}</realdamage>
					<temporarydamage>{temporaryDamage}</temporarydamage>
					<maxhitpoints>{maxHitPoints}</maxhitpoints>
				</hitpoints>;
			return hpXML;
		}
		
		public function buildFromXML(xml:XML):void
		{
			hps							= parseInt(xml.hitpoints.hps);
			realDamage					= parseInt(xml.hitpoints.realdamage);
			temporaryDamage				= parseInt(xml.hitpoints.temporaryDamage);
			maxHitPoints				= parseInt(xml.hitpoints.maxhitpoints);
		}
		
	}
}